var inputE = document.querySelector('#number');
var countE = document.querySelector('#count');
var resultE = document.querySelector('#result');
// 得到一个1-100的随机数
var random = parseInt(Math.random() * 100) + 1;
var count = 0;


// 1. 事件处理对象
var btn = document.querySelector('#commit');
var btn_reset = document.querySelector('#reset');

// 2. 事件处理函数
function judgeNumber(){
    count++;
    countE.innerHTML = count;

    var userGuess = parseInt(inputE.value);
    if(isNaN(userGuess)){
        resultE.innerHTML = "输入错误，请重新输入！";
        resultE.style = "color : red";
        return;
    }

    if(userGuess < random){
        resultE.innerHTML = "猜小了，再大点!";
        resultE.style = "color : blue";
    }else if(userGuess > random){
        resultE.innerHTML = "猜大了，再小点!";
        resultE.style = "color : blue";
    }else {
        resultE.innerHTML = "恭喜你，猜对了！";
        resultE.style = "color : green"
    }
    inputE.value = "";
}

function resetGame(){
    count = 0;
    countE.innerHTML = count;

    inputE.value = "";
    resultE.innerHTML = "";

    random = parseInt(Math.random() * 100) + 1;
}

// 3. 将 事件、事件处理对象、事件处理函数 bind
inputE.onkeypress = function(e) {
    if (e.key === 'Enter') {
        judgeNumber();
    }
}
btn.onclick = judgeNumber;
btn_reset.onclick = resetGame;
